#
# peerscape.profile.text
#
# Copyright 2008 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
Get or set "about" text for personal or group profile.

>>> from peerscape import DB
>>> db = DB()
>>> p = db.init_persona('Test User')

>>> db.get_profile_text(p)
(None, False)

>>> db.set_profile_text(p, 'Hello friends!')
>>> db.get_profile_text(p)
(u'Hello friends!', False)

>>> db.get_last_event(p)['comment']
u'Updated profile text.'
>>> db.post_event(p, 'Testing...', public=False)
>>> db.set_profile_text(p, 'Hello friends!')
>>> db.get_last_event(p)['comment']
u'Testing...'

>>> db.set_profile_text(p, 'Hello world!', public=True)
>>> db.get_profile_text(p)
(u'Hello world!', True)

>>> db.get_last_event(p)['comment']
u'Updated profile text.'
>>> db.post_event(p, 'Testing...')
>>> db.set_profile_text(p, 'Hello world!', public=True)
>>> db.get_last_event(p)['comment']
u'Testing...'

>>> db.clear_profile_text(p)
>>> db.get_profile_text(p)
(None, False)

>>> db.get_last_event(p)['comment']
u'Testing...'

>>> db.close()
"""

from __future__ import with_statement

PUBLIC_PATH = '/public/about.txt'
RESTRICTED_PATH = '/restricted/about.txt'

class ProfileTextDBMixin(object):
    def get_profile_text(self, iid):
        """Return the text (or None) and a boolean indicating if it is public."""
        with self.read_transaction():
            text = self.get_text(iid, PUBLIC_PATH)
            if text is not None:
                return text, True

            try:
                cipher = self.get_group_cipher(iid)
            except LookupError:
                return None, False

            text = self.get_text(iid, RESTRICTED_PATH, cipher=cipher)
            return text, False

    def set_profile_text(self, iid, text, public=False):
        """Store the provided Unicode text."""
        with self.write_transaction():
            if self.get_profile_text(iid) == (text, public):
                return

            if text is not None and public:
                self.put(iid, PUBLIC_PATH, text)
            else:
                self.delete(iid, PUBLIC_PATH)

            if text is not None and not public:
                cipher = self.get_group_cipher(iid)
                self.put(iid, RESTRICTED_PATH, text, cipher=cipher)
            else:
                self.delete(iid, RESTRICTED_PATH)

            if text:
                self.post_event(iid, 'Updated profile text.', public=public)
            else:
                self.post_nonevent(iid)

    def clear_profile_text(self, iid):
        """Clear any stored text."""
        self.set_profile_text(iid, None)

if __name__ == '__main__':
    import doctest
    doctest.testmod()
